package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecAbroadEditBean;
import cn.ffcs.uec.staff.model.UecAbroad;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecAbroadEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecAbroadEditBean bean = new UecAbroadEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecAbroad uecAbroad;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecAbroadEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {

		
	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecAbroadEditWindow().setTitle("新增出国出境情况");
			bean.getUecAbroadEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecAbroadEditWindow().setTitle("查看出国出境情况");
			uecAbroad = (UecAbroad) arg.get("uecAbroad");
			if (null != uecAbroad) {
				PubUtil.fillBeanFromPo(uecAbroad, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecAbroadEditWindow(),true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecAbroadEditWindow().setTitle("编辑出国出境情况");
			bean.getUecAbroadEditDiv().setHeight("30%");
			uecAbroad = (UecAbroad) arg.get("uecAbroad");
			if (null != uecAbroad) {
				PubUtil.fillBeanFromPo(uecAbroad, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecAbroad = UecAbroad.newInstance();
			PubUtil.fillPoFromBean(bean, uecAbroad);
			dateTransToString();
			uecAbroad.setStaffId(uecStaffInfo.getStaffId());
			uecAbroad.addOnly();
			Events.postEvent("onOK", this.self, uecAbroad);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecAbroad);
			dateTransToString();
			uecAbroad.updateOnly();
			Events.postEvent("onOK", this.self, uecAbroad);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecAbroadEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		 if (StrUtil.isNullOrEmpty(bean.getAbroadway().getValue())) {
		 return "请填写出国出境目的";
		 } else if (StrUtil.isNullOrEmpty(bean.getAbroaddate().getValue())) {
		 return "请填写出国(出境)时间";
		 }else if (StrUtil.isNullOrEmpty(bean.getAbroadarea().getValue())) {
			 return "请填写所去国家";
		 }else if (StrUtil.isNullOrEmpty(bean.getAbroadprodate().getValue())) {
			 return "请填写审批时间";
		 }
		return null;
	}
	/**
	 * 将Date类型时间转成String类型
	 * Date --> String
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getAbroaddate().getValue())) {
			// 出国(出境)时间
			uecAbroad.setAbroaddate(formatter.format(bean.getAbroaddate().getValue()));
		}
		if(!StrUtil.isNullOrEmpty(bean.getAbroadreturn().getValue())){
			//回国时间
			uecAbroad.setAbroadreturn(formatter.format(bean.getAbroadreturn().getValue()));
		}
		if(!StrUtil.isNullOrEmpty(bean.getAbroadprodate().getValue())){
			//审批时间
			uecAbroad.setAbroadprodate(formatter.format(bean.getAbroadprodate().getValue()));
		}

	}
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 出国(出境)时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecAbroad.getAbroaddate()) {
			bean.getAbroaddate()
					.setValue(sdf.parse(uecAbroad.getAbroaddate()));
		}
		//回国时间
		if (null != uecAbroad.getAbroadreturn()) {
			bean.getAbroadreturn()
					.setValue(sdf.parse(uecAbroad.getAbroadreturn()));
		}
		//审批时间
		if (null != uecAbroad.getAbroadprodate()) {
			bean.getAbroadprodate()
					.setValue(sdf.parse(uecAbroad.getAbroadprodate()));
		}
	}

}
